﻿window.zsmm = {};

zsmm.Toast = {
	isopen: true,
	ins: null,
	intval: 0,
	New: function (msg, remain) {
		var that = this;
		if (this.isopen && this.ins) {
			that.clear();
		}
		this.instance(msg);
		remain || (remain = 3);
		this.intval = setTimeout(function () {
			that.ins.css('opacity', 0);
		}, remain * 1000);
	},
	instance: function (msg) {
		this.ins = $('<div id="global-toast"></div>');
		this.ins.appendTo('body');
		this.ins.html(msg);
		this.ins.css('margin-left', (-this.ins.width() / 2) + 'px');
		this.ins.show();
		var that = this;
		this.ins[0].addEventListener("transitionend", function () {
			that.clear();
		});
		this.ins[0].addEventListener("webkitTransitionEnd", function () {
			that.clear();
		});
	},
	clear: function () {
		this.ins.remove();
		this.ins = null;
		clearTimeout(this.intval);
	}
};

zsmm.Popup = {
	isopen: true,
	ins: null,
	New: function (obj, close) {
		var that = this;
		if (this.isopen && this.ins) {
			that.clear();
			window.console.log("isOpen");
		}
		this.instance(obj, close);
	},
	instance: function (obj, close) {
		if (!(obj.length > 0)) return false;
		var that = this;
		this.ins = $('<div class="popup-wrapper"></div>');
		this.ins.appendTo('body');
		obj = obj.clone().removeClass("hidden").prop("outerHTML");;
		this.ins.html('<div class="popup-layout vertical">' + obj + '</div>');
		this.ins.show();
		that.init(close);
	},
	init: function (close) {
		var that = this;
		var toClose = (typeof (close) == "undefined") ? 1 : close;
		if (toClose == 0) return false;
		this.ins.find(".popup-layout").click(function (e) {
			e.preventDefault();
			that.clear();
		});
		this.ins.find(".popup-layout>div").click(function (e) {
			e.preventDefault();
			e.stopPropagation();
		});
	},
	clear: function () {
		this.ins.remove();
		this.ins = null;
	}
};